package com.example.book.repository

import androidx.annotation.WorkerThread
import com.example.book.model.Book
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@WorkerThread
class BookRepository @Inject constructor(private val bookDao: BookDao, private val bookWebService: BookWebService){

    fun getAll(): Flow<List<Book>> {

        bookWebService.getBooks().execute().body().apply {
            this?.let { bookDao.insert(it) }
        }

        return bookDao.getAll()
    }

    fun get(isbn: String): Flow<Book>? {
        return bookDao.get(isbn)
    }

    fun insert(vararg books: Book) {
        bookDao.insert(*books)
    }

    fun insert(books: List<Book>) {
        bookDao.insert(books)
    }

    fun delete(isbn: String){
        //bookDao.delete(Book(isbn,"",""))
        bookDao.delete(isbn)

        bookWebService.deleteBook(isbn)
    }

    fun deleteAll(){
        bookDao.deleteAll()
    }

    fun update(isbn: String,price: String){
        bookDao.update(BookDao.Price(isbn,price))
    }
}